<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>asserts.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_asserts_asserts.js.html">asserts.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities to check the preconditions, postconditions and
<a name="line17"></a> * invariants runtime.
<a name="line18"></a> *
<a name="line19"></a> * Methods in this package should be given special treatment by the compiler
<a name="line20"></a> * for type-inference. For example, &lt;code&gt;goog.asserts.assert(foo)&lt;/code&gt;
<a name="line21"></a> * will restrict &lt;code&gt;foo&lt;/code&gt; to a truthy value.
<a name="line22"></a> *
<a name="line23"></a> * The compiler has an option to disable asserts. So code like:
<a name="line24"></a> * &lt;code&gt;
<a name="line25"></a> * var x = goog.asserts.assert(foo()); goog.asserts.assert(bar());
<a name="line26"></a> * &lt;/code&gt;
<a name="line27"></a> * will be transformed into:
<a name="line28"></a> * &lt;code&gt;
<a name="line29"></a> * var x = foo();
<a name="line30"></a> * &lt;/code&gt;
<a name="line31"></a> * The compiler will leave in foo() (because its return value is used),
<a name="line32"></a> * but it will remove bar() because it assumes it does not have side-effects.
<a name="line33"></a> *
<a name="line34"></a> * @author agrieve@google.com (Andrew Grieve)
<a name="line35"></a> */
<a name="line36"></a>
<a name="line37"></a>goog.provide(&#39;goog.asserts&#39;);
<a name="line38"></a>goog.provide(&#39;goog.asserts.AssertionError&#39;);
<a name="line39"></a>
<a name="line40"></a>goog.require(&#39;goog.debug.Error&#39;);
<a name="line41"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line42"></a>goog.require(&#39;goog.string&#39;);
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * @define {boolean} Whether to strip out asserts or to leave them in.
<a name="line47"></a> */
<a name="line48"></a>goog.define(&#39;goog.asserts.ENABLE_ASSERTS&#39;, goog.DEBUG);
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/**
<a name="line53"></a> * Error object for failed assertions.
<a name="line54"></a> * @param {string} messagePattern The pattern that was used to form message.
<a name="line55"></a> * @param {!Array.&lt;*&gt;} messageArgs The items to substitute into the pattern.
<a name="line56"></a> * @constructor
<a name="line57"></a> * @extends {goog.debug.Error}
<a name="line58"></a> * @final
<a name="line59"></a> */
<a name="line60"></a>goog.asserts.AssertionError = function(messagePattern, messageArgs) {
<a name="line61"></a>  messageArgs.unshift(messagePattern);
<a name="line62"></a>  goog.debug.Error.call(this, goog.string.subs.apply(null, messageArgs));
<a name="line63"></a>  // Remove the messagePattern afterwards to avoid permenantly modifying the
<a name="line64"></a>  // passed in array.
<a name="line65"></a>  messageArgs.shift();
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * The message pattern used to format the error message. Error handlers can
<a name="line69"></a>   * use this to uniquely identify the assertion.
<a name="line70"></a>   * @type {string}
<a name="line71"></a>   */
<a name="line72"></a>  this.messagePattern = messagePattern;
<a name="line73"></a>};
<a name="line74"></a>goog.inherits(goog.asserts.AssertionError, goog.debug.Error);
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/** @override */
<a name="line78"></a>goog.asserts.AssertionError.prototype.name = &#39;AssertionError&#39;;
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Throws an exception with the given message and &quot;Assertion failed&quot; prefixed
<a name="line83"></a> * onto it.
<a name="line84"></a> * @param {string} defaultMessage The message to use if givenMessage is empty.
<a name="line85"></a> * @param {Array.&lt;*&gt;} defaultArgs The substitution arguments for defaultMessage.
<a name="line86"></a> * @param {string|undefined} givenMessage Message supplied by the caller.
<a name="line87"></a> * @param {Array.&lt;*&gt;} givenArgs The substitution arguments for givenMessage.
<a name="line88"></a> * @throws {goog.asserts.AssertionError} When the value is not a number.
<a name="line89"></a> * @private
<a name="line90"></a> */
<a name="line91"></a>goog.asserts.doAssertFailure_ =
<a name="line92"></a>    function(defaultMessage, defaultArgs, givenMessage, givenArgs) {
<a name="line93"></a>  var message = &#39;Assertion failed&#39;;
<a name="line94"></a>  if (givenMessage) {
<a name="line95"></a>    message += &#39;: &#39; + givenMessage;
<a name="line96"></a>    var args = givenArgs;
<a name="line97"></a>  } else if (defaultMessage) {
<a name="line98"></a>    message += &#39;: &#39; + defaultMessage;
<a name="line99"></a>    args = defaultArgs;
<a name="line100"></a>  }
<a name="line101"></a>  // The &#39;&#39; + works around an Opera 10 bug in the unit tests. Without it,
<a name="line102"></a>  // a stack trace is added to var message above. With this, a stack trace is
<a name="line103"></a>  // not added until this line (it causes the extra garbage to be added after
<a name="line104"></a>  // the assertion message instead of in the middle of it).
<a name="line105"></a>  throw new goog.asserts.AssertionError(&#39;&#39; + message, args || []);
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Checks if the condition evaluates to true if goog.asserts.ENABLE_ASSERTS is
<a name="line111"></a> * true.
<a name="line112"></a> * @template T
<a name="line113"></a> * @param {T} condition The condition to check.
<a name="line114"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line115"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line116"></a> * @return {T} The value of the condition.
<a name="line117"></a> * @throws {goog.asserts.AssertionError} When the condition evaluates to false.
<a name="line118"></a> */
<a name="line119"></a>goog.asserts.assert = function(condition, opt_message, var_args) {
<a name="line120"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; !condition) {
<a name="line121"></a>    goog.asserts.doAssertFailure_(&#39;&#39;, null, opt_message,
<a name="line122"></a>        Array.prototype.slice.call(arguments, 2));
<a name="line123"></a>  }
<a name="line124"></a>  return condition;
<a name="line125"></a>};
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Fails if goog.asserts.ENABLE_ASSERTS is true. This function is useful in case
<a name="line130"></a> * when we want to add a check in the unreachable area like switch-case
<a name="line131"></a> * statement:
<a name="line132"></a> *
<a name="line133"></a> * &lt;pre&gt;
<a name="line134"></a> *  switch(type) {
<a name="line135"></a> *    case FOO: doSomething(); break;
<a name="line136"></a> *    case BAR: doSomethingElse(); break;
<a name="line137"></a> *    default: goog.assert.fail(&#39;Unrecognized type: &#39; + type);
<a name="line138"></a> *      // We have only 2 types - &quot;default:&quot; section is unreachable code.
<a name="line139"></a> *  }
<a name="line140"></a> * &lt;/pre&gt;
<a name="line141"></a> *
<a name="line142"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line143"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line144"></a> * @throws {goog.asserts.AssertionError} Failure.
<a name="line145"></a> */
<a name="line146"></a>goog.asserts.fail = function(opt_message, var_args) {
<a name="line147"></a>  if (goog.asserts.ENABLE_ASSERTS) {
<a name="line148"></a>    throw new goog.asserts.AssertionError(
<a name="line149"></a>        &#39;Failure&#39; + (opt_message ? &#39;: &#39; + opt_message : &#39;&#39;),
<a name="line150"></a>        Array.prototype.slice.call(arguments, 1));
<a name="line151"></a>  }
<a name="line152"></a>};
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * Checks if the value is a number if goog.asserts.ENABLE_ASSERTS is true.
<a name="line157"></a> * @param {*} value The value to check.
<a name="line158"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line159"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line160"></a> * @return {number} The value, guaranteed to be a number when asserts enabled.
<a name="line161"></a> * @throws {goog.asserts.AssertionError} When the value is not a number.
<a name="line162"></a> */
<a name="line163"></a>goog.asserts.assertNumber = function(value, opt_message, var_args) {
<a name="line164"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; !goog.isNumber(value)) {
<a name="line165"></a>    goog.asserts.doAssertFailure_(&#39;Expected number but got %s: %s.&#39;,
<a name="line166"></a>        [goog.typeOf(value), value], opt_message,
<a name="line167"></a>        Array.prototype.slice.call(arguments, 2));
<a name="line168"></a>  }
<a name="line169"></a>  return /** @type {number} */ (value);
<a name="line170"></a>};
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/**
<a name="line174"></a> * Checks if the value is a string if goog.asserts.ENABLE_ASSERTS is true.
<a name="line175"></a> * @param {*} value The value to check.
<a name="line176"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line177"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line178"></a> * @return {string} The value, guaranteed to be a string when asserts enabled.
<a name="line179"></a> * @throws {goog.asserts.AssertionError} When the value is not a string.
<a name="line180"></a> */
<a name="line181"></a>goog.asserts.assertString = function(value, opt_message, var_args) {
<a name="line182"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; !goog.isString(value)) {
<a name="line183"></a>    goog.asserts.doAssertFailure_(&#39;Expected string but got %s: %s.&#39;,
<a name="line184"></a>        [goog.typeOf(value), value], opt_message,
<a name="line185"></a>        Array.prototype.slice.call(arguments, 2));
<a name="line186"></a>  }
<a name="line187"></a>  return /** @type {string} */ (value);
<a name="line188"></a>};
<a name="line189"></a>
<a name="line190"></a>
<a name="line191"></a>/**
<a name="line192"></a> * Checks if the value is a function if goog.asserts.ENABLE_ASSERTS is true.
<a name="line193"></a> * @param {*} value The value to check.
<a name="line194"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line195"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line196"></a> * @return {!Function} The value, guaranteed to be a function when asserts
<a name="line197"></a> *     enabled.
<a name="line198"></a> * @throws {goog.asserts.AssertionError} When the value is not a function.
<a name="line199"></a> */
<a name="line200"></a>goog.asserts.assertFunction = function(value, opt_message, var_args) {
<a name="line201"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; !goog.isFunction(value)) {
<a name="line202"></a>    goog.asserts.doAssertFailure_(&#39;Expected function but got %s: %s.&#39;,
<a name="line203"></a>        [goog.typeOf(value), value], opt_message,
<a name="line204"></a>        Array.prototype.slice.call(arguments, 2));
<a name="line205"></a>  }
<a name="line206"></a>  return /** @type {!Function} */ (value);
<a name="line207"></a>};
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * Checks if the value is an Object if goog.asserts.ENABLE_ASSERTS is true.
<a name="line212"></a> * @param {*} value The value to check.
<a name="line213"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line214"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line215"></a> * @return {!Object} The value, guaranteed to be a non-null object.
<a name="line216"></a> * @throws {goog.asserts.AssertionError} When the value is not an object.
<a name="line217"></a> */
<a name="line218"></a>goog.asserts.assertObject = function(value, opt_message, var_args) {
<a name="line219"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; !goog.isObject(value)) {
<a name="line220"></a>    goog.asserts.doAssertFailure_(&#39;Expected object but got %s: %s.&#39;,
<a name="line221"></a>        [goog.typeOf(value), value],
<a name="line222"></a>        opt_message, Array.prototype.slice.call(arguments, 2));
<a name="line223"></a>  }
<a name="line224"></a>  return /** @type {!Object} */ (value);
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/**
<a name="line229"></a> * Checks if the value is an Array if goog.asserts.ENABLE_ASSERTS is true.
<a name="line230"></a> * @param {*} value The value to check.
<a name="line231"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line232"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line233"></a> * @return {!Array} The value, guaranteed to be a non-null array.
<a name="line234"></a> * @throws {goog.asserts.AssertionError} When the value is not an array.
<a name="line235"></a> */
<a name="line236"></a>goog.asserts.assertArray = function(value, opt_message, var_args) {
<a name="line237"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; !goog.isArray(value)) {
<a name="line238"></a>    goog.asserts.doAssertFailure_(&#39;Expected array but got %s: %s.&#39;,
<a name="line239"></a>        [goog.typeOf(value), value], opt_message,
<a name="line240"></a>        Array.prototype.slice.call(arguments, 2));
<a name="line241"></a>  }
<a name="line242"></a>  return /** @type {!Array} */ (value);
<a name="line243"></a>};
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/**
<a name="line247"></a> * Checks if the value is a boolean if goog.asserts.ENABLE_ASSERTS is true.
<a name="line248"></a> * @param {*} value The value to check.
<a name="line249"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line250"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line251"></a> * @return {boolean} The value, guaranteed to be a boolean when asserts are
<a name="line252"></a> *     enabled.
<a name="line253"></a> * @throws {goog.asserts.AssertionError} When the value is not a boolean.
<a name="line254"></a> */
<a name="line255"></a>goog.asserts.assertBoolean = function(value, opt_message, var_args) {
<a name="line256"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; !goog.isBoolean(value)) {
<a name="line257"></a>    goog.asserts.doAssertFailure_(&#39;Expected boolean but got %s: %s.&#39;,
<a name="line258"></a>        [goog.typeOf(value), value], opt_message,
<a name="line259"></a>        Array.prototype.slice.call(arguments, 2));
<a name="line260"></a>  }
<a name="line261"></a>  return /** @type {boolean} */ (value);
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Checks if the value is a DOM Element if goog.asserts.ENABLE_ASSERTS is true.
<a name="line267"></a> * @param {*} value The value to check.
<a name="line268"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line269"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line270"></a> * @return {!Element} The value, likely to be a DOM Element when asserts are
<a name="line271"></a> *     enabled.
<a name="line272"></a> * @throws {goog.asserts.AssertionError} When the value is not a boolean.
<a name="line273"></a> */
<a name="line274"></a>goog.asserts.assertElement = function(value, opt_message, var_args) {
<a name="line275"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; (!goog.isObject(value) ||
<a name="line276"></a>      value.nodeType != goog.dom.NodeType.ELEMENT)) {
<a name="line277"></a>    goog.asserts.doAssertFailure_(&#39;Expected Element but got %s: %s.&#39;,
<a name="line278"></a>        [goog.typeOf(value), value], opt_message,
<a name="line279"></a>        Array.prototype.slice.call(arguments, 2));
<a name="line280"></a>  }
<a name="line281"></a>  return /** @type {!Element} */ (value);
<a name="line282"></a>};
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>/**
<a name="line286"></a> * Checks if the value is an instance of the user-defined type if
<a name="line287"></a> * goog.asserts.ENABLE_ASSERTS is true.
<a name="line288"></a> *
<a name="line289"></a> * The compiler may tighten the type returned by this function.
<a name="line290"></a> *
<a name="line291"></a> * @param {*} value The value to check.
<a name="line292"></a> * @param {function(new: T, ...)} type A user-defined constructor.
<a name="line293"></a> * @param {string=} opt_message Error message in case of failure.
<a name="line294"></a> * @param {...*} var_args The items to substitute into the failure message.
<a name="line295"></a> * @throws {goog.asserts.AssertionError} When the value is not an instance of
<a name="line296"></a> *     type.
<a name="line297"></a> * @return {!T}
<a name="line298"></a> * @template T
<a name="line299"></a> */
<a name="line300"></a>goog.asserts.assertInstanceof = function(value, type, opt_message, var_args) {
<a name="line301"></a>  if (goog.asserts.ENABLE_ASSERTS &amp;&amp; !(value instanceof type)) {
<a name="line302"></a>    goog.asserts.doAssertFailure_(&#39;instanceof check failed.&#39;, null,
<a name="line303"></a>        opt_message, Array.prototype.slice.call(arguments, 3));
<a name="line304"></a>  }
<a name="line305"></a>  return value;
<a name="line306"></a>};
<a name="line307"></a>
<a name="line308"></a>
<a name="line309"></a>/**
<a name="line310"></a> * Checks that no enumerable keys are present in Object.prototype. Such keys
<a name="line311"></a> * would break most code that use {@code for (var ... in ...)} loops.
<a name="line312"></a> */
<a name="line313"></a>goog.asserts.assertObjectPrototypeIsIntact = function() {
<a name="line314"></a>  for (var key in Object.prototype) {
<a name="line315"></a>    goog.asserts.fail(key + &#39; should not be enumerable in Object.prototype.&#39;);
<a name="line316"></a>  }
<a name="line317"></a>};
</pre>


</body>
</html>
